package com.gzhryc.shared_device.oem.h5.mng.controllers.bxscn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyCabinet;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyCabinetSlot;
import com.gzhryc.shared_device.oem.code.device.BXSCNCacheManager;

/**
 * 便携式电池充电柜缓存
 * @version 2023-11-12
 */
public class PortableEnergyCabinetCacheController {

    @Route(value = { "/bxscn/device/cache/list.html" })
    public String list(HttpServletRequest request) {
        return "/bxscn/device/cache/list.html";
    }

    @Route(value = "/bxscn/device/cache/list.data")
    public ListResult<PortableEnergyCabinet> listData(HttpServletRequest request) {
        ListResult<PortableEnergyCabinet> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<PortableEnergyCabinet> data = BXSCNCacheManager.self().findBXSCNDevice();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = "/bxscn/device/slot/cache/list.data")
    public ListResult<PortableEnergyCabinetSlot> slotListData(HttpServletRequest request) {
        ListResult<PortableEnergyCabinetSlot> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<PortableEnergyCabinetSlot> data = BXSCNCacheManager.self().findBXSCNSlotDevice();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/bxscn/device/cache/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            try {
                BXSCNCacheManager.self().removePortableEnergyCabinet(deviceSn);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/bxscn/device/slot/cache/del.do" })
    public AjaxResult doSlotDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request,"slotNum");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            try {
                BXSCNCacheManager.self().removePortableEnergyCabinetSlot(deviceSn,slotNum);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
